package eirkdbd.lab1.test;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import eirkdbd.lab1.data.Team;
import eirkdbd.lab1.data.TeamDataLoader;
import eirkdbd.lab1.fuzzyqueries.BipolarQuery;
import eirkdbd.lab1.fuzzyqueries.circumstances.Circumstance;
import eirkdbd.lab1.fuzzyqueries.circumstances.TheSameLeague;
import eirkdbd.lab1.fuzzyqueries.helpers.BipolarQueriesResultsPrinter;
import eirkdbd.lab1.membership.MembershipFunction;
import eirkdbd.lab1.membership.functions.ComposedMembershipFunction;
import eirkdbd.lab1.membership.functions.MembershipFunctionFactory;
import eirkdbd.lab1.membership.functions.helpers.norms.Norm;
import eirkdbd.lab1.membership.functions.helpers.norms.implications.Implication;
import eirkdbd.lab1.membership.functions.helpers.norms.implications.ImplicationFactory;
import eirkdbd.lab1.membership.functions.helpers.norms.tnorms.Tnorm;
import eirkdbd.lab1.membership.functions.helpers.norms.tnorms.TnormFactory;


public class UseExample {


	public static void main(String[] args) throws SQLException, IOException {
//		if (args.length == 0){
//			System.out.println("Run application with help argument to get app help");
//			return;
//		}
//		
//		if (args.length == 1 ){
//			if (args[0].equals("help"))
//				System.out.println(getHelpString());
//			else
//				System.out.println("Can't start application with one criterium");
//		}
//		else if (args.length == 2 || args.length == 3){
//			parseArguemnts(args); 
//		}
//		else
//			System.out.println("Too much arguemnts");
//		
//		return;
//		MembershipFunction C = MembershipFunctionFactory.GOALSCORING.getMembershipFunction();
//		
//		List<MembershipFunction> mfs = new ArrayList<MembershipFunction>();
//		mfs.add(MembershipFunctionFactory.WINNING.getMembershipFunction());
//		mfs.add(MembershipFunctionFactory.NOT_LOSING.getMembershipFunction());
//		List<Norm> norms = new ArrayList<Norm>();
//		norms.add(TnormFactory.PRODUCT.getTnorm());
//		MembershipFunction P = new ComposedMembershipFunction(mfs, norms);
//		
//		Circumstance W = new TheSameLeague();
		TeamDataLoader tdl;
		List<Team>  teams;
		try {
			tdl = new TeamDataLoader();
			teams = tdl.getTeamData();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return;
		}
//		try {
//		// execute query and print results
//		BipolarQuery bq = new BipolarQuery(C, P,  teams, TnormFactory.MINIMUM.getTnorm(), ImplicationFactory.KLEENEDIENES.getImplication() );
//		BipolarQueriesResultsPrinter.printResults(bq.execute(), true);
//		}
//		catch (IllegalArgumentException iae){
//			System.out.println(iae.getMessage());
//		}
		MembershipFunction C = MembershipFunctionFactory.GOALSCORING.getMembershipFunction();
		MembershipFunction P = MembershipFunctionFactory.NOT_LOSING.getMembershipFunction();
		Tnorm tn = TnormFactory.MINIMUM.getTnorm();
		Implication imp = ImplicationFactory.GOGUEN.getImplication();
		
		BipolarQuery bq = new BipolarQuery(C, P, teams, tn, imp);
		BipolarQueriesResultsPrinter.printResults(bq.execute(), false);
		
	}

	private static void parseArguemnts(String[] args) {
		String Cname = args[0];
		String Pname = args[1];
		boolean ommitZeros = true;
		if (args.length == 3){
			ommitZeros = Boolean.valueOf(args[2]);
		}
		MembershipFunction C = null;
		MembershipFunction P = null;
		for (MembershipFunctionFactory mff : MembershipFunctionFactory.values() ){
			if(mff.toString().toLowerCase().equals(Cname.toLowerCase()))
				C = mff.getMembershipFunction();
			if(mff.toString().toLowerCase().equals(Pname.toLowerCase()))
				P = mff.getMembershipFunction();			
		}
		if (C == null){
			System.out.println(Cname + " is not valid criterium name");
			return;
		}
		if (P == null){
			System.out.println(Pname + "is not valid criterium name");
			return;
		}
		
		printResults(C, P, ommitZeros);
	}

	private static void printResults(MembershipFunction C, MembershipFunction P, boolean ommitZeros) {
		//get team data
		TeamDataLoader tdl;
		List<Team>  teams;
		try {
			tdl = new TeamDataLoader();
			teams = tdl.getTeamData();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return;
		}		
		// create bipolar query
		try {
		// execute query and print results
		BipolarQuery bq = new BipolarQuery(C, P, teams, TnormFactory.MINIMUM.getTnorm(), ImplicationFactory.KLEENEDIENES.getImplication());
		BipolarQueriesResultsPrinter.printResults(bq.execute(), ommitZeros);
		}
		catch (IllegalArgumentException iae){
			System.out.println(iae.getMessage());
		}
		
		
		
	}

	private static String getHelpString() {
		String help = "Start application with two of the following criteriums (case doesn't matter). First criterium will be mandatory criterium (C) second one will be wanted criterium (P):";
		StringBuilder sb = new StringBuilder(help);
		
		for (MembershipFunctionFactory mff : MembershipFunctionFactory.values()){
			sb.append("\n" + mff.toString() );
		}
		sb.append("\nIf you want to show records with 0 membership degree pass 'false' as 3rd argument");
		sb.append("\nCriterium description can be found in docuemntation");
		return sb.toString();
	}
	

	
}
